package com.gmgx.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Component
public class FileTransferUtil {
    // 输入：MultipartFile对象、String path
    // 输出：转存进新的文件
    public String transferFile(MultipartFile originalFile, String path){
        String oldFileName = originalFile.getOriginalFilename();
        String suffix = oldFileName.substring(oldFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
        File pathFile = new File(path);
        if (!pathFile.exists()){
            pathFile.mkdirs();
        }
        String newPath = pathFile + "/" + newFileName;
        try {
            originalFile.transferTo(new File(newPath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return newPath;
    }
}
